//单词搜索
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param board string字符串vector 
     * @param word string字符串 
     * @return bool布尔型
     */
    bool search(vector<string>& board, string& word, int i, int j, int k)
    {
        if(i < 0 || i == board.size() || j < 0 || j == board[0].size() || board[i][j] != word[k])
            return false;
        if(k == word.size())
            return true;
        char temp = board[i][j];
        board[i][j] = '#';
        
        if(search(board, word, i - 1, j, k + 1) || search(board, word, i + 1, j, k + 1) ||
           search(board, word, i, j - 1, k + 1) || search(board, word, i, j + 1, k + 1))
            return true;
        board[i][j] = temp;
        return false;
    }
    
    bool exist(vector<string>& board, string word) {
        // write code here
        int m = board.size();
        int n = board[0].size();

        for(int i = 0; i < m; i++)
        {
            for(int j = 0; i < n; i++)
            {
                if(board[i][j] == word[0] && search(board, word, i, j, 0))
                    return true;
            }
        }

        return false;
    }
};